package com.ykzb.user.dao.impl;

import javax.annotation.Resource;

import com.ykzb.user.dao.IUserDao;
import com.ykzb.user.mapper.UserMapper;
import com.ykzb.user.model.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;

@SuppressWarnings("restriction")
@Repository("userDao")
public class UserDaoImpl implements IUserDao
{
    @Resource
	private UserMapper userMapper;

	@Override
	public User selectLoginUser(String userName, String password) {
		if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)){
			return null;
		}else{
			return userMapper.selectLoginUser(userName,password);
		}
	}

	@Override
	public boolean isMobileExists(String mobile) throws Exception {
		if (StringUtils.isEmpty(mobile)){
			return false;
		}else{
			Integer userNum = userMapper.selectCountUserByMobile(mobile);
			if (userNum!=null && userNum > 0){
				return true;
			}else{
				return false;
			}
		}
	}

	@Override
	public void insert(User user) throws Exception {
		if (user!=null){
			userMapper.insert(user);
		}
	}

	@Override
	public User selectUserByMobile(String mobile) throws Exception {
		if (StringUtils.isEmpty(mobile)){
			return null;
		}else{
			List<User> userList = userMapper.selectUserByMobile(mobile);
			if (CollectionUtils.isEmpty(userList)){
				return null;
			}else{
				return userList.get(0);
			}
		}
	}
}